package com.wish.g01.file.controller;

import com.qiniu.http.Response;
import com.qiniu.storage.model.DefaultPutRet;
import com.wish.g01.file.service.QiNiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Classname QiNiuController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/5/27 19:30
 * @Created by wupenghao
 */
@RestController
@Slf4j
@RequestMapping("qiniu")
public class QiNiuController {

    
    @Autowired
    QiNiService qiNiService;
    /**
     * 文件上传请求
     */
    /**
     *
     * @param file  上传的文件
     * @param key   文件名
     * @param token  上传凭证
     * @return
     * @throws Exception
     */
    @PostMapping("upload")
    public DefaultPutRet qiniuupload(MultipartFile file, String key, String token) throws Exception {
        // 判断 文件 大小 是否采用流的形式
        long size = file.getSize();
        if (size > 4 * 1024 * 1024){
            InputStream inputStream = file.getInputStream();
          return qiNiService.put(inputStream, size, key, token);
        }
//        单文件上传
        return qiNiService.uploadFeil(file, key, token);
    }

    /**
     * 生成七牛云的token
     *
     * @return
     */
    @GetMapping("getToken")
    public String getToken() {
        //获取七牛云密钥
        return qiNiService.getToken();
    }

}
